package com.junyidao.game.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.junyidao.game.pay.AlipayAccount;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.pojo.SysUser;
import com.junyidao.game.service.AccountChargeService;
import com.junyidao.game.util.NumUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;


/**
 * Created by zzh on 2016/9/23.
 * 文件描述 ：充值
 */
@Controller
@RequestMapping("/accountCharge")
public class AccountChargeController extends BaseController {

    @Autowired
    private AccountChargeService accountChargeService;

    @RequestMapping
    @Action(desc = "账号充值")
    public ModelAndView list() {
        ModelAndView result = new ModelAndView("finance/account_charge");
        SysUser user = LoginInfo.getUser();
        result.addObject("accountName", user.getAccountName());
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/charge")
    @Action(desc = "获取渠道列表", sysPriv = false)
    public Map<String, Object> charge(String accountName, Double amount, Integer payType) throws Exception {
//        Integer total_fee = NumUtils.multiply(amount, 100).intValue();
        Integer total_fee = 1;
        Map<String, String> map = accountChargeService.accountCharge(accountName, total_fee, payType);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    @Action(desc = "获取渠道列表", sysPriv = false)
    public Map<String, Object> query(Integer payType, String outTradeNo) throws Exception {
        String tradeStatus = accountChargeService.queryPayStatus(payType, outTradeNo);
        return resultSucess(tradeStatus);
    }

    @Test
    public void test() throws Exception {
        //商户系统调用支付宝预下单接口alipay.trade.precreate，获得该订单二维码图片地址
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayAccount.ALIPAY_GATEWAY,
                AlipayAccount.APPID, AlipayAccount.APP_PRIVATE_KEY, "json", "UTF-8",
                AlipayAccount.ALIPAY_PUBLIC_KEY);
        //创建API对应的request类
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setBizContent("{" +
                "    \"out_trade_no\":\"20160924010101089\"," +
                "    \"total_amount\":12," +
                "    \"subject\":\"Iphone66G\"," +
                "    \"timeout_express\":\"90m\"" +
                "  }");//设置业务参数
        System.out.printf("---------" + request.getBizContent());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.printf("二维码:" + response.getQrCode());
        System.out.printf("订单号:" + response.getOutTradeNo());
    }

    @Test
    public void query() throws Exception {
        //商户系统调用支付宝预下单接口alipay.trade.precreate，获得该订单二维码图片地址
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayAccount.ALIPAY_GATEWAY,
                AlipayAccount.APPID, AlipayAccount.APP_PRIVATE_KEY, "json", "UTF-8",
                AlipayAccount.ALIPAY_PUBLIC_KEY);
        //创建API对应的request类
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "    \"out_trade_no\":\"20160924010101089\"" +
                "  }");//设置业务参数
        System.out.printf("---------" + request.getBizContent());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.printf("二维码:" + response.getTradeStatus());
    }

}
